package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.util.ImportUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: liangWeiQUQ
 * @Description: 预约模块的信息管理
 * @DateTime: 2021/3/1 15:36
 **/
@RestController
@Slf4j
@RequestMapping("ordersetting")
public class OrderSettingController {
    //注入service接口
     @DubboReference
    private OrderSettingService orderSettingService;

     /**
      * @Method
      * @Author 传闻中的梁大侠
      * @Version  1.0
      * @Description 上传excel批量设置可预约人数
      * @param
      * @Return
      * @Exception
      * @Date
      */
    @PostMapping("upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile) {
        //获取源文件名称
        String filename = multipartFile.getOriginalFilename();
        if (StringUtils.isEmpty(filename)) {
            return new Result(false, "文件名称不能为空");
        }
        //文件读取
        List<List<Object>> lists;
        try {
            if (filename.endsWith(".xlsx")) { //07版
                lists = ImportUtil.read2007Excel(multipartFile.getInputStream());
            } else if (filename.endsWith(".xls")) { //03版
                lists = ImportUtil.read2003Excel(multipartFile.getInputStream());
            } else {
                return new Result(false, "非法文件名称");
            }
            //将 lists转换为 List<OrderSetting>
            List<OrderSetting> orderSettingList = parseToOrderSetting(lists);
            //rpc调用添加 List<OrderSetting>
            orderSettingService.add(orderSettingList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.UPLOAD_FAIL);
        }
        return new Result(true, MessageConst.UPLOAD_SUCCESS);
    }
    /**
     * @Method 
     * @Author 传闻中的梁大侠
     * @Version  1.0
     * @Description  将excel读取的数据转换为List<OrderSetting></>
     * @param
     * @Return 
     * @Exception 
     * @Date  
     */
    private List<OrderSetting> parseToOrderSetting(List<List<Object>> lists) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<OrderSetting> orderSettingList = new ArrayList<>();
        for (int i = 1; i <lists.size(); i++) { //行 第一行数据忽略，因为是表头
            List<Object> cells = lists.get(i);
            //cells不建议遍历，防止越界数据(脏数据)
            String orderDate = (String) cells.get(0); //日期
            String num = (String) cells.get(1); //可预约人数
            OrderSetting orderSetting = new OrderSetting();
            orderSetting.setOrderDate(sdf.parse(orderDate));
            orderSetting.setNumber(Integer.valueOf(num));
            orderSettingList.add(orderSetting);
        }
        return orderSettingList;
    }
    /**
     * @Method
     * @Author 传闻中的梁大侠
     * @Version  1.0
     * @Description 根据日期查询当月的预约信息
     * @param
     * @Return
     * @Exception
     * @Date
     */
    @GetMapping("findByDate")
    public Result findByDate(String startTime, String endTime) {
        List<OrderSetting> orderSettings = orderSettingService.findByDate(startTime, endTime);
        List<Map<String, Object>> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        for (OrderSetting orderSetting : orderSettings) {
            Date orderDate = orderSetting.getOrderDate();
            calendar.setTime(orderDate);
            Map<String, Object> map = new HashMap<>();
            map.put("date", calendar.get(Calendar.DAY_OF_MONTH));
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            list.add(map);
        }
        return new Result(true, MessageConst.ACTION_SUCCESS,list);
    }
    /**
     * @Method
     * @Author 传闻中的梁大侠
     * @Version  1.0
     * @Description  设置可预约人数
     * @param
     * @Return
     * @Exception
     * @Date
     */
    @PostMapping("editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        orderSettingService.editNumByDate(orderSetting);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }
}
